import { Promise } from 'drunk-base';
import { HOST } from '../../../service/Config';
import { getJsonWithToken, postJsonWithToken, putJsonWithToken, deleteJsonWithToken } from '../../../service/HttpClient';
import { IAccountModel } from './AccountModel';
import { IPageMetaData } from '../../ListModel';
import { IMediaModel } from '../media/MediaModel';

export function getAccount(id: string): Promise<IAccountModel> {
    return getJsonWithToken(`${HOST}/sys/accounts/${id}`);
}

export function getAccountList(page: number, size = 20): Promise<{ meta: IPageMetaData; data: IAccountModel[] }> {
    return getJsonWithToken(`${HOST}/sys/accounts?page=${page}&size=${size}`);
}

export function createAccount(data: IAccountModel): Promise<IAccountModel> {
    return postJsonWithToken(`${HOST}/sys/accounts`, data);
}

export function updateAccount(id: string, data: { password: string; contactInfo: string; note: string }): Promise<IAccountModel> {
    return putJsonWithToken(`${HOST}/sys/accounts/${id}`, data);
}

export function deleteAccount(id: string) {
    return deleteJsonWithToken(`${HOST}/sys/accounts/${id}`);
}

export function search(name: string, startTime: number, endTime: number, page = 0, size = 20): Promise<{ data: IAccountModel[], meta: IPageMetaData }> {
    return getJsonWithToken(`${HOST}/sys/accounts?name=${name}&startTime=${startTime}&endTime=${endTime}&page=${page}&size=${size}`);
}

export function getMediaOptionsByName(name: string) {
    return getJsonWithToken(`${HOST}/sys/meiti?name=${name}&page=0&size=100`)
        .then((res: { data: IMediaModel[], meta: IPageMetaData }) =>
            res.data.map(item => ({ id: item.id, name: item.name }))
        );
}

export function getMyAccount(): Promise<IAccountModel> {
    return getJsonWithToken(`${HOST}/auth/me`);
}
